import { Observable } from "./Observable";

export class BaseObject extends Observable {
  private values_: { [key: string]: any } = {};
  constructor() {
    super();
  }

  public get(key: string) {
    let value;
    if (this.values_.hasOwnProperty(key)) {
      value = this.values_[key];
    }
    return value;
  }

  /**
   * set value
   * @param key key's name
   * @param value key's value
   * @param opt_silent update without trigger event
   */
  public set(key: string, value: any, opt_silent?: boolean) {
    if (opt_silent) this.values_[key] = value;

    const oldValue = this.values_[key];
    this.values_[key] = value;
    if (oldValue !== value) {
      this.notify(key, oldValue);
    }
  }

  public setProperties(values) {
    for (const key in values) {
      this.set(key, values[key]);
    }
  }

  private notify(key: string, value: any){
      this.dispatchEvent("change:" + key, value);
  }
}
